package com.company.math;

/**
 * @author jrue
 * @description https://leetcode-cn.com/problems/powx-n/
 * @date 2019/11/21 10:47
 * 实现 pow(x, n) ，即计算 x 的 n 次幂函数。
 */
public class PowxN {

    public double fastPow(double x,long n) {
        if (n == 0) return 1.0;
        double half = fastPow(x, n / 2);
        if (n % 2 == 0) {
            return half * half;
        } else {
            return half * half * x;
        }

    }


    public double myPow(double x, int n) {
        long N = n;
        if (N < 0) {
            x = 1 / x;
            N = -N;
        }
        return fastPow(x,n);
    }

}
